﻿/*----------------------------------------------------------------
  //Copyright (C) 2012 赵国伟
  //文件名：UserFavoriteManage.cs
  //文件功能描述：收藏夹管理；
  //创建人：赵国伟
  //生成时间：2012/03/18 16:15:00
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Shopping.Common;
using Shopping.Model;
using Shopping.DAL;

namespace Shopping.BLL
{
    public class UserFavoriteManage
    {
        private readonly DALUserFavorites dalFav = new DALUserFavorites();

        #region "收藏指定的商品； by 赵国伟 2012-04-24"
        /// <summary>
        /// 收藏指定的商品；
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="GoodsID"></param>
        /// <returns></returns>
        public ReturnValue Add(int userId, int GoodsID)
        {
            ReturnValue ret = new ReturnValue();
            //检查是否已添加过该商品；
            if (dalFav.QueryList(1, "UserID=" + userId + " and GoodsID=" + GoodsID, "") != null)
            {
                ret.Message = "您已收藏过该商品";
                return ret;
            }
            UserFavorites d = new UserFavorites();
            d.UserID = userId;
            d.GoodsID = GoodsID;
            d.CollectDate = DateTime.Now;

            if (dalFav.Add(d) > 0)
            {
                ret.Result = ResultType.Success;
                ret.Message = "保存成功！";
            }
            return ret;

        }
        #endregion

        #region "删除指定用户指定商品的收藏 by 赵国伟 2012-04-24"
        /// <summary>
        /// 删除指定用户指定商品的收藏
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="GoodsID"></param>
        /// <returns></returns>
        public ReturnValue Delete(int UserId, int GoodsID)
        {
            ReturnValue ret = new ReturnValue(ResultType.Fail,"删除失败！");
            if (dalFav.Delete(UserId, GoodsID) > 0)
            {
                ret.Result = ResultType.Success;
                ret.Message = "删除成功！";
            }
            return ret;
        }
        #endregion


        #region "获得指定用户的关注列表 "
        /// <summary>
        /// 获得指定用户的关注列表；
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<UserFavoriteInfo> QueryList(int userId)
        {
            List<UserFavorites> source = dalFav.QueryList(50, "UserID=" + userId, " CollectDate desc");
            List<UserFavoriteInfo> target = null;

            if(source != null)
            {
                target = new List<UserFavoriteInfo>();
                GoodsManage bllGoods = new GoodsManage();
                Dictionary<int, GoodsInfo> dictGoods = bllGoods.ListToHash(bllGoods.GetFavoriteGoods(userId));

                foreach(UserFavorites s in source)
                {
                    UserFavoriteInfo t = new UserFavoriteInfo();
                    t.FavoriteInfo = s;
                    if (dictGoods.ContainsKey(s.GoodsID))
                        t.Goods = dictGoods[s.GoodsID];
                    target.Add(t);
                }
            }
            return target;
        }
        #endregion


    }
}
